﻿using UnityEngine;
using System.Collections;

public class ApuntarCanon : MonoBehaviour
{
	public Rigidbody bubble;
	private float angAct;
	// Use this for initialization
	void Start ()
	{
		angAct = Mathf.PI / 2;
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetMouseButton (0)) {
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			Vector3 point = ray.origin + ray.direction;
			point.z = transform.position.z;
			transform.LookAt (point, new Vector3 (0, 0, -1));
			transform.Rotate(Quaternion.AngleAxis(90,new Vector3(1,0,0)).eulerAngles);
			//transform.LookAt (point);
			//Debug.DrawRay (ray.origin, ray.direction, Color.yellow);
			Debug.DrawLine(ray.origin, point, Color.yellow);
			Debug.Log(ray.direction);
		}
		if (Input.GetMouseButtonUp (0)) {
			//Vector3 posBub = new Vector3(transform.position.x,transform.position.y+0.5,transform.position.z);
			Rigidbody bubbleClone = (Rigidbody) Instantiate(bubble, transform.position, transform.rotation);
			//Vector3 forwdNorm = new Vector3(transform.forward.x,transform.forward.y,transform.forward.z);
			Vector3 forwdNorm = transform.up.normalized;
			//forwdNorm.Normalize();
			bubbleClone.velocity = forwdNorm * 2f;
			Debug.Log(forwdNorm.magnitude);
		}
	}
}
